import { format } from "date-fns";
import axios from '@/env/axios'
import apis from "@/env/apis";

export const deleteEmptyParam = data => {
  let newData = {};
  Object.keys(data).forEach(item => {
    if (data[item]) {
      newData[item] = data[item];
    }
  });
  return newData;
};

export const formatterBannerOs = (row, column, cellValue) => {
  // console.log(row, 'row');
  if (row) {
    return format(new Date(row * 1000), "yyyy-MM-dd HH:mm:ss");
  } else {
    return "--";
  }
};

export const fileByBase64 = file => {
  return new Promise((resolve, reject) => {
    try {
      var reader = new FileReader();
      // 传入一个参数对象即可得到基于该参数对象的文本内容
      reader.readAsDataURL(file);
      reader.onload = function(e) {
        // target.result 该属性表示目标对象的DataURL
        resolve(e.target.result);
      };
    } catch (error) {
      reject(error);
    }
  });
};

export const downLoadTempFunc = async (res, fileName) => {
  const blob = new Blob([res], {
    type: "application/vnd.ms-excel;charset=UTF-8",
  });
  const downloadurl = window.URL.createObjectURL(blob);
  let anchor = document.createElement("a");
  anchor.href = downloadurl;
  anchor.download = fileName || "导入文件模板";
  anchor.click();
  window.URL.revokeObjectURL(blob);
};

// 经纬度转换成三角函数中度分表形式。
function rad(d) {
  return (d * Math.PI) / 180.0;
}

// 根据经纬度计算距离，参数分别为第一点的纬度，经度；第二点的纬度，经度
export const getDistance = (lat1, lng1, lat2, lng2) => {
  var radLat1 = rad(lat1);
  var radLat2 = rad(lat2);
  var a = radLat1 - radLat2;
  var b = rad(lng1) - rad(lng2);
  var s =
    2 *
    Math.asin(
      Math.sqrt(
        Math.pow(Math.sin(a / 2), 2) +
          Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)
      )
    );
  s = s * 6378.137; // EARTH_RADIUS;
  s = Math.round(s * 10000) / 10000; //输出为公里

  var distance = s;
  var distance_str = "";

  if (parseInt(distance) >= 1) {
    distance_str = distance.toFixed(1) + "km";
  } else {
    distance_str = distance * 1000 + "m";
  }

  //s=s.toFixed(4);

  return s;
}


export const addOperationLog = async (data) => {
  const res = await axios.post(apis.operationLogSave, data)
  if (res.retCode === 2000) {
    console.log('添加日志成功');
  }
}